new Vue({
    /*
    let vue = new Vue({
                el:'#app',
                data:{
                    setmeal:{},//套餐信息
                    orderInfo:{
                        setmealId:id,
                        sex:'1'
                    }//预约信息
                },

                mounted(){

                }
            });
     */
    el: "#app",
    data: {
        //套餐信息
        setmeal: {},

        //预约信息
        orderInfo: {
            setmealId: id,
            sex: '1'
        },

        //地址信息
        addressList: [],

        flag:false,

        mapDrawerVisible:false,

        //抽屉的上滑方向 bottom to top
        direction: 'btt',

        // 用户可选择的 出行方式选择
        tripMode:["驾车" , "公交" , "步行"],

        //用户选择的出行方式
        userTripMode:"",

        localLocation:"117.33548442175478,31.86686365442126",

        currentMap:"",

        wayToGet:""



    },
    methods: {

        //发送验证码
        sendValidateCode() {
            //获取用户输入的手机号
            let telephone = this.orderInfo.telephone;
            //调用js方法，校验输入的手机号是否合法
            if (!checkTelephone(telephone)) {
                //校验不通过，提示错误信息
                this.$message.error("请输入正确的手机号");
                return false;
            }
            //在按钮上显示30秒倒计时效果
            validateCodeButton = $("#validateCodeButton")[0];//锁定dom对象
            clock = window.setInterval(doLoop, 1000);//定时器方法，可以实现每隔指定的时间调用指定的方法
            //发送ajax请求，为用户发送手机验证码
            axios.post("/validateCode/send4Order.do?telephone=" + telephone)
                .then((res) => {
                    if (!res.data.flag) {
                        //短信验证码发送失败
                        this.$message.error(res.data.message);
                    }
                });
        },


        //提交预约
        submitOrder() {
            //获取用户输入的手机号
            let telephone = this.orderInfo.telephone;
            //调用js方法，校验输入的手机号是否合法
            if (!checkTelephone(telephone)) {
                //校验不通过，提示错误信息
                this.$message.error("请输入正确的手机号");
                return false;
            }
            //对用户输入的身份证号进行校验
            let idCard = this.orderInfo.idCard;
            if (!checkIdCard(idCard)) {
                //身份证号输入不合法
                this.$message.error("身份证号码输入错误，请重新输入");
                return false;
            }
            //发送ajax请求，提交表单数据到后台进行业务处理
            axios.post("/order/submit.do", this.orderInfo)
                .then((res) => {
                    if (res.data.flag) {
                        //预约成功，跳转到成功页面
                        window.location.href = "orderSuccess.html?orderId=" + res.data.data;
                    } else {
                        //预约失败，提示信息
                        this.$message.error(res.data.message);
                    }
                });
        },

        //开启地图
        openMap() {


            //开启地图时，依据用户是否选择了体检地址进行动态的显示
            // 用户如果选择了地址，那么就显示用户当前的地址到已选择的体检中心的地址

            // 如果没有选择体检地址 ， 那么就显示所有已有的体检地址让用户进行预览
            let _this = this;

            _this.mapDrawerVisible = true;

            //将地图对象定义在最外面 ，在条件判断中做相应的调整
            let map = new BMap.Map("mapContainer");
            map.centerAndZoom("合肥", 13);

            //开启鼠标滚轮缩放
            map.enableScrollWheelZoom(true);

            //添加控件
            map.addControl(new BMap.NavigationControl()); // 平移缩放控件

            this.currentMap = map;

            if(this.orderInfo.address){
                //说明用户选择了地址
                let targetAddress;

                for (let i = 0; i < _this.addressList.length; i++) {
                    //获得当前用户选择的是哪个地址
                    if(_this.addressList[i].id == this.orderInfo.address){
                        targetAddress = _this.addressList[i];
                    }

                }

                //获取到目标地址的经纬度
                let location = targetAddress.location;
                let lon = location.split(",")[0];
                let lat = location.split(",")[1];


                //创建标记点位
                // point 指定标记物应该在哪里
                let point = new BMap.Point(lon, lat);

                // 创建标注
                let marker = new BMap.Marker(point);

                marker.setLabel(new BMap.Label(targetAddress.name+"<br/>" + targetAddress.address , {offset: new BMap.Size(20, -10)}));



                // 将标注添加到地图中
                map.addOverlay(marker);


            }else{
                //用户没有选择地址


                // 百度地图API功能
                /*        var map = new BMap.Map("allmap");
                var point = new BMap.Point(116.404, 39.915);
                map.centerAndZoom(point, 15);

                //创建小狐狸
                var pt = new BMap.Point(116.417, 39.909);
                var myIcon = new BMap.Icon("/jsdemo/img/fox.gif", new BMap.Size(300,157));
                var marker2 = new BMap.Marker(pt,{icon:myIcon});  // 创建标注
                map.addOverlay(marker2);
*/
                //用户没有选择地址


                //那么就遍历所有的 addressList 对地图进行标注
                for (let i = 0; i < _this.addressList.length; i++) {

                    let tempAddress = _this.addressList[i];

                    let long = tempAddress.location.split(",")[0];
                    let lat = tempAddress.location.split(",")[1];

                    let point = new BMap.Point(long , lat);


                    let marker = new BMap.Marker(point);

                    marker.setLabel(new BMap.Label(tempAddress.name+"<br/>" + tempAddress.address , {offset: new BMap.Size(20, -10)}));

                    map.addOverlay(marker);


                }

            }






        },

        // 关闭地图抽屉时的回调函数
        // done 是一个 方法的对象 ， 用于关闭 Drawer
        closeMap(done){


            this.userTripMode = "";
            this.currentMap = "";

            done();


        },

        addressChangeInside(addressId){

            let selectedAddress;

            for (let i = 0; i < this.addressList.length; i++) {

                let temp = this.addressList[i];
                if(temp.id == addressId){
                    selectedAddress = temp;
                    break;
                }

            }

            this.currentMap.clearOverlays();

            let long = selectedAddress.location.split(",")[0];
            let lat = selectedAddress.location.split(",")[1];

            let point = new BMap.Point(long , lat);

            let marker = new BMap.Marker(point);

            marker.setLabel(new BMap.Label(selectedAddress.name+"<br/>" + selectedAddress.address , {offset: new BMap.Size(20, -10)}));

            this.userTripMode = "";
            this.wayToGet = "";
            this.currentMap.addOverlay(marker);

        },

        tripModeChange(){

            if(this.wayToGet){
                this.wayToGet = "";
            }


            if( ! this.orderInfo.address){
                this.$message.warning("请先选择您要进行体检的地址");
                this.userTripMode = "";
                return;
            }


          let _this = this;


          let mode = this.userTripMode;

          let long = this.localLocation.split(",")[0];
          let lat = this.localLocation.split(",")[1];
          let startPoint = new BMap.Point(long, lat);

          let endAddressId = _this.orderInfo.address;

          let endAddress;
            for (let i = 0; i < _this.addressList.length; i++) {

                let tempAddress = _this.addressList[i];

                if(tempAddress.id == endAddressId){
                    endAddress = tempAddress;
                    break;
                }


            }

            let endLong = endAddress.location.split(",")[0];
            let endLat = endAddress.location.split(",")[1];
            let endPoint = new BMap.Point(endLong, endLat);


          if(mode == "驾车"){
              //驾车出行

              this.wayToGet = new BMap.DrivingRoute(_this.currentMap, {
                  renderOptions: {
                      map: _this.currentMap,
                      autoViewport: true
                  }
              });

              this.wayToGet.search(startPoint , endPoint);

          }else if(mode == "步行"){
              //步行
              this.wayToGet = new BMap.WalkingRoute(_this.currentMap, {
                  renderOptions: {
                      map: _this.currentMap,
                      autoViewport: true
                  }
              });

              this.wayToGet.search(startPoint, endPoint);

          }else{
              //公交

              this.wayToGet = new BMap.TransitRoute(_this.currentMap, {
                  renderOptions: {
                      map: _this.currentMap,
                      autoViewport: true

                  },
                  // 配置跨城公交的换成策略为优先出发早
                  intercityPolicy: BMAP_INTERCITY_POLICY_EARLY_START,
                  // 配置跨城公交的交通方式策略为飞机优先
                  transitTypePolicy: BMAP_TRANSIT_TYPE_POLICY_AIRPLANE
              });

              this.wayToGet.search(startPoint, endPoint);

          }

        },


    },

    created: function () {

        let _this = this;
        //发送请求获取套餐信息
        axios.post("/setmeal/findById.do?id=" + id)
            .then((res) => {
                if (res.data.flag) {
                    this.setmeal = res.data.data;
                }
            });

        //发送请求获取地址列表信息
        axios.get("/mobile/address/findAll.do").then(function(resp){

            if (resp.data.flag) {

                _this.addressList = resp.data.data;


            }else{

                _this.$message.warning(resp.data.message);

            }



        })

    }


});